package net.linku.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

public class PinyinUtil {

    /**
     * 将汉字转换为拼音
     *
     * @param hanzi 汉字字符串
     * @return 对应的拼音字符串
     */
    public static String convertToPinyin(String hanzi) {
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for (char c : hanzi.toCharArray()) {
            if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
                try {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    if (pinyinArray != null && pinyinArray.length > 0) {
                        pinyin.append(pinyinArray[0]);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                pinyin.append(c);
            }
        }

        return pinyin.toString();
    }

    /**
     * 提取拼音的首字母
     *
     * @param pinyin 拼音字符串
     * @return 拼音的首字母字符串
     */
    public static String getFirstLetters(String pinyin) {
        StringBuilder firstLetters = new StringBuilder();
        for (char c : pinyin.toCharArray()) {
            if (Character.isLetter(c)) {
                firstLetters.append(Character.toUpperCase(c)).append(" ");
            }
        }
        return firstLetters.toString().trim();
    }

    public static String convertToPinyinInitials(String chineseName) {
        StringBuilder initials = new StringBuilder();

        // 分解中文姓名为单个字符
        for (char c : chineseName.toCharArray()) {
            // 获取每个字符的拼音数组
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
            if (pinyinArray != null && pinyinArray.length > 0) {
                // 取拼音的首字母
                initials.append(pinyinArray[0].charAt(0));
            } else {
                // 如果该字符不是中文，则直接加入
                initials.append(c);
            }
        }

        return initials.toString();
    }


    public static String getFirstInitial(String chineseName) {
        // 获取第一个字符
        char firstChar = chineseName.charAt(0);

        // 获取第一个字符的拼音数组
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(firstChar);

        if (pinyinArray != null && pinyinArray.length > 0) {
            // 取拼音的首字母
            return String.valueOf(pinyinArray[0].charAt(0)).toUpperCase();
        } else {
            // 如果第一个字符不是中文，则直接返回大写的该字符
            return String.valueOf(firstChar).toUpperCase();
        }
    }


    public static void main(String[] args) {
        String hanzi = "Silence田";
        String pinyin = convertToPinyin(hanzi);
        System.out.println("汉字：" + hanzi);
        System.out.println("拼音：" + pinyin);
        System.out.println("首字母：" + getFirstLetters(pinyin));
        String chineseName = "田祥东";
        String initials = convertToPinyinInitials(chineseName);
        System.out.println("Initials: " + initials);  // 输出 "txd"
        String input = "你好"; // 或者 "ni hao"
        String result = getFirstInitial(input);
        System.out.println("Initials: " + result); // 输出 "N"
    }
}

